<template>
	<div>
		<div class="step-3">
		<div class="step_half">
			<el-row>
			  <el-col :span="8" style="text-align: right;vertical-align: middle;line-height: 32px;"><span>试卷名称：</span></el-col>
			  <el-col :span="16"><el-input size="mini" v-model="pageData.name" placeholder="请输入试卷名称"></el-input></el-col>
			</el-row>
			<el-row>
			  <el-col :span="8" style="text-align: right;vertical-align: middle;line-height: 32px;"><span>试卷分类：</span></el-col>
			  <el-col :span="16"><el-input size="mini" v-model="pageData.type" placeholder="请输入考试分类"></el-input></el-col>
			</el-row>
			<el-row>
			  <el-col :span="8" style="text-align: right;vertical-align: middle;line-height: 32px;"><span>答卷时长：</span></el-col>
			  <el-col :span="16"><el-input size="mini" style="width: 100px;" v-model="pageData.time" placeholder=""></el-input>&nbsp;分钟</el-col>
			</el-row>
			<el-row>
			  <el-col :span="8" style="text-align: right;vertical-align: middle;line-height: 32px;"><span>及格分数：</span></el-col>
			  <el-col :span="16"><el-input size="mini" style="width: 100px;" v-model="pageData.passMark" placeholder=""></el-input></el-col>
			</el-row>
			<el-row>
			  <el-col :span="8" style="text-align: right;vertical-align: middle;line-height: 32px;"><span>考试说明：</span></el-col>
			  <el-col :span="16"><el-input type="textarea" :autosize="{ minRows: 4, maxRows: 8}" placeholder="请输入内容" v-model="pageData.inText"></el-input></el-col>
			</el-row>
		</div>
		<div class="step_half">
			<el-row>
			  <el-col :span="24" style="padding-left:10px;line-height: 28px;border-left:5px solid #1A8CFE;"><span>试卷信息</span></el-col>
			</el-row>
			<el-row>
				<el-col :span="24">
					<el-card class="box-card">
					  <div slot="header" class="clearfix">
					    <span class="title">{{pageData.name}}</span>
					    <el-button @click="editionPaper"  style="float: right; padding: 3px 0" type="text">编辑试卷<i class="el-icon-edit"></i></el-button>
					  </div>
					 <p>总分：<span>{{pageData.score || 0}}</span></p>
					 <p>总题目数量：<span>{{pageData.listLen || 0}}</span></p>
					</el-card>
				</el-col>
			</el-row>
		</div>
	</div>
	<el-row style="margin-top:60px;">
		<el-col :span="24" style="text-align: center;">
			<el-button @click="savePage" type="warning">保存当前试卷</el-button>
		</el-col>
	</el-row>
</div>
</template>

<script>
    import { paperCreate,paperUpdate,Login} from '@/assets/util/api.js'
  export default {
    data() {
      return {
      	checkNum:1,
      	checkPaperVisible:false,
      	access_token:"",
      	pageData:{
      		name:"",
      		type:"",
      		score:"",
      		time:"",
      		inText:"",
      		title:"",
      		passMark:"",
      	},
      	allList:"",
      };
    },
    mounted(){
      this.access_token = this.inStorage("get","access_token");
      this.questionType = this.inStorage("get","questionType");
      let pageItem = this.inStorage("get","pageItem");
      let pageData = this.inStorage("get","allList");
      this.allList = this.inStorage("get","allList");
      this.pageData.name = pageItem.pageName;
      this.pageData.type = pageItem.pageType;
      this.pageData.score = pageItem.score;
      this.pageData.time = pageItem.time;
      this.pageData.inText = pageItem.inText;
      this.pageData.listLen = pageItem.listLen;
      this.pageData.passMark = parseInt(pageItem.score * 0.6);
    },
    methods: {
     checkItem(idx){
     	this.checkNum = idx;
     },
     querySearch(queryString, cb) {
        var restaurants = this.restaurants;
        var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants;
        // 调用 callback 返回建议列表的数据
        cb(results);
      },
      handleSelect(item) {
        console.log(item);
      },
        checkPaper(){
        	this.checkPaperVisible = true;
        },
        handleClose(done) {
	        this.$confirm('确认关闭？')
	          .then(_ => {
	            done();
	          })
	          .catch(_ => {});
	    },
	    paperCreate(questions){
	    	var _this = this;
	    	if(!this.pageData.time){
	    		this.$message.error("答卷时常不可为空！");
	    		return;
	    	}
     	var params = {
     		access_token:this.access_token,
     		name:this.pageData.name,
     		subject:this.pageData.type,
     		limit_time:this.pageData.time*60,
     		questions:questions,
     		attach:this.pageData.inText,
     		pass_score:this.pageData.passMark
     	}
     	paperCreate(this,params).then((res)=>{
     		if(res.data.code){
				_this.$message.error(_this.publicMsg(res.data.code),res.data.message);
			}
     		if(res.data.state){
     			_this.$message.success("添加成功!");
     			_this.inStorage("clear","pageItem");
     			_this.inStorage("clear","allList");
     			_this.RouterTo(_this,'/testPaperLibrary');
     		}
     	}).catch((res)=>{
     		console.log(res)
     	})
     },
     paperUpdate(paper_no,questions){
     	var _this = this;
	    	if(!this.pageData.time){
	    		this.$message.error("答卷时常不可为空！");
	    		return;
	    	}
     	var params = {
     		access_token:this.access_token,
     		paper_no:paper_no,
     		name:this.pageData.name,
     		subject:this.pageData.type,
     		limit_time:this.pageData.time*60,
     		questions:questions,
     		attach:this.pageData.inText,
     		pass_score:this.pageData.passMark,
     		is_vaild:0,
     	}
     	paperUpdate(this,params).then((res)=>{
     		if(res.data.code){
				_this.$message.error(_this.publicMsg(res.data.code),res.data.message);
			}
     		if(res.data.state){
     			_this.$message.success("添加成功!");
     			_this.inStorage("clear","pageEditor");
     			_this.inStorage("clear","allList");
     			_this.RouterTo(_this,'/testPaperLibrary');
     		}
     	}).catch((res)=>{
     		console.log(res)
     	})
     },
	 savePage(){
	 	var arrStr = [];
	 	this.allList.forEach((str,idx,arr) =>{
//	 		console.log(str);
	 		let strlist = [];
	 		str.list.forEach((listr,liidx,liarr)=>{
	 			let li =listr.quest_no + ':' + listr.full_score;
	 			strlist.push(li);
//	 			console.log(strlist)
	 		})
//	 		console.log(arrStr.concat(strlist))
	 		arrStr = arrStr.concat(strlist);
	 	})
//	 	console.log(arrStr)
		if(this.$route.query.step){
     		this.paperCreate(arrStr);
     	}else{
     		let paperNo = this.inStorage("get","pageItem").paperNo;
     		this.paperUpdate(paperNo,arrStr);
     	}
	   
	 },
	 editionPaper(){
	 	let pageItem = this.inStorage("get","pageItem");
	 	pageItem.time = this.pageData.time;
	 	pageItem.inText = this.pageData.inText;
	 	this.inStorage("set","allList",this.allList);
	 	this.inStorage("set","pageItem",pageItem);
	 	this.$router.go(-1);
	 }
    }
  }
 
</script>
<style lang="scss" scoped>
  .step-3{
  	width: 100%;
  	padding-top: 40px;
  	font-family: 'MicrosoftYaHei','微软雅黑','Helvetica Neue','Hiragino Sans GB','WenQuanYi Micro Hei',sans-serif;
    color: #666;
    font-size: 14px;
    overflow: hidden;
    display: flex;
    justify-content: space-between;
  	.step_half{
  		width:48%;
  		float: left;
  	}
  	.el-row{
  		margin-bottom: 10px;
  		.box-card{
  			.title{
  				display: inline-block;
  				max-width: 70%;
  				overflow: hidden;
  				white-space: nowrap;
  				text-overflow: ellipsis;
  			}
  		}
  	}
  }
</style>

